import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

typedef Widget ListViewBuilder(
    BuildContext context, int index, dynamic itemData);
typedef void OnRefresh();
typedef void OnLoading();
typedef void OnInitFinish();

class CjyListView extends StatefulWidget {
  final ListViewBuilder itemView;
  final OnRefresh onRefresh;
  final OnLoading onLoading;
  final CjyController cjyController;
  final OnInitFinish initFinish;

  /// 外层如果是Column()布局，请包裹Expanded()
  CjyListView(
      {this.itemView,
      this.onRefresh,
      this.onLoading,
      this.cjyController,
      this.initFinish});

  @override
  State<StatefulWidget> createState() {
    return CjyListViewState();
  }
}

class CjyListViewState extends State<CjyListView> {
  RefreshController refreshController = RefreshController();

  List dataList = [];

  @override
  void initState() {
    widget.cjyController.init(this, dataList, refreshController);
    if(widget.initFinish != null)widget.initFinish();
    super.initState();
  }

  @override
  void dispose() {
    refreshController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
      enablePullDown: true,
      enablePullUp: true,
      header: WaterDropHeader(
        complete: Text("数据刷新"),
        failed: Text("没有数据了"),
      ),
      footer: CustomFooter(
        builder: (BuildContext context, LoadStatus mode) {
          Widget body;
          if (mode == LoadStatus.idle) {
            body = Text("上拉加载");
          } else if (mode == LoadStatus.loading) {
            body = CupertinoActivityIndicator();
          } else if (mode == LoadStatus.failed) {
            body = Text("加载失败,点击重试");
          } else if (mode == LoadStatus.canLoading) {
            body = Text("松手,加载更多");
          } else {
            body = Text("没有更多数据了");
          }
          return Container(
            height: 55.h,
            child: Center(child: body),
          );
        },
      ),
      controller: refreshController,
      onRefresh: widget.onRefresh,
      onLoading: widget.onLoading,
      child: ListView.builder(
        itemBuilder: (BuildContext context, int index) =>
            widget.itemView(context, index, dataList[index]),
        itemCount: dataList.length,
      ),
    );
  }

  void onFinishLoading(List tempList) {
    if (tempList.isEmpty) {
      refreshController.loadNoData();
    } else {
      setState(() {
        dataList.addAll(tempList);
      });
      refreshController.loadComplete();
    }
  }

  Future<void> onFinishRefresh(List tempList) async {
    refreshController.loadComplete();
    setState(() {
      dataList.clear();
      dataList.addAll(tempList);
    });
    refreshController.refreshCompleted();
  }
}

class CjyController {
  CjyListViewState state;

  init(CjyListViewState state, List dataList,
      RefreshController refreshController) {
    this.state = state;
  }

  void onFinishLoading(List tempList) {
    state.onFinishLoading(tempList);
  }

  void onFinishRefresh(List tempList) {
    state.onFinishRefresh(tempList);
  }
}
